package com.jd.ty.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.jd.ty.dao.FieldDAO;
import com.jd.ty.dao.constant.QueryConstants;
import com.jd.ty.dao.util.HqlCriterions;
import com.jd.ty.pojo.Field;
import com.jd.ty.service.exception.ServiceException;
import com.jd.ty.util.TemplateUtil;
import com.ty.xsl.writer.ExcelWriter;

public class ExportService {
	private static final String FREEMARKER_PATH = "WEB-INF/classes/template/export/";
	public static final String ROOT_KEY_FIELDS = "fields";
	public static final String ROOT_KEY_DATA = "data";
	public static final String ROOT_KEY_FUNC_EVAL = "eval";
	private ExcelWriter excelWriter;
	private FieldDAO fieldDAO;

	public String exportExcel(String templateName,
			Map<String, List> multiSheet, List<Field> fields)
			throws ServiceException {

		String template = ServletActionContext.getServletContext().getRealPath(
				"/");
		template += FREEMARKER_PATH + templateName + ".ftl";

		Map<String, String> htmlMap = new HashMap<String, String>();

		for (String sheetName : multiSheet.keySet()) {
			Map<String, Object> root = new HashMap<String, Object>();
			root.put(ROOT_KEY_FIELDS, fields);
			root.put(ROOT_KEY_DATA, multiSheet.get(sheetName));
			root.put(ROOT_KEY_FUNC_EVAL, new com.jd.ty.web.BeanFieldEval());

			Map<String, Object> data = new HashMap<String, Object>();
			data.put("root", root);
			String html = TemplateUtil.process(template, data);
			htmlMap.put(sheetName, html);
		}

		return excelWriter.writeHTML(htmlMap);
	}

	public String exportExcel(String templateName, Map<String, Object> multiSheet)
			throws ServiceException {
		String template = ServletActionContext.getServletContext().getRealPath(
				"/");
		template += FREEMARKER_PATH + templateName + ".ftl";
		Map<String, String> htmlMap = new HashMap<String, String>();
		for (String sheetName : multiSheet.keySet()) {
			Map<String, Object> root = new HashMap<String, Object>();
			root.put(ROOT_KEY_DATA, multiSheet.get(sheetName));
			root.put(ROOT_KEY_FUNC_EVAL, new com.jd.ty.web.BeanFieldEval());

			Map<String, Object> data = new HashMap<String, Object>();
			data.put("root", root);
			String html = TemplateUtil.process(template, data);
			htmlMap.put(sheetName, html);
		}

		return excelWriter.writeHTML(htmlMap);
	}

	public void setExcelWriter(ExcelWriter excelWriter) {
		this.excelWriter = excelWriter;
	}

	public List<Field> getFields(Byte type) {
		HqlCriterions hc = new HqlCriterions();
		hc.and(QueryConstants.Field.REPORT_TYPE, type, QueryConstants.EQ);
		return fieldDAO.getByQuery(hc.getHqlString());
	}

	public List<Field> getFieldsByIds(String fieldIds) {
		String[] ids = fieldIds.split(",");
		HqlCriterions hc = new HqlCriterions();

		for (int i = 0; i < ids.length; i++) {
			hc.or(QueryConstants.Field.FIELD_ID, ids[i], QueryConstants.EQ);
		}
		return fieldDAO.getByQuery(hc.getHqlString());
	}

	public void setFieldDAO(FieldDAO fieldDAO) {
		this.fieldDAO = fieldDAO;
	}

}
